﻿using Iworld.Author.Model;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Manager
{

    /// <summary>
    /// 用户的登陆信息池
    /// </summary>
    public class AuthorLoginInfoPond
    {
        #region 私有字段

        /// <summary>
        /// 保持登录状态的时间
        /// </summary>
        public readonly static int keepLiveSeconds = 30;

        #endregion

        #region 静态方法

        /// <summary>
        /// 声明用户登入
        /// </summary>
        /// <param name="userId">用户信息的存储指针</param>
        /// <returns>返回身份标识</returns>
        public static string AddInfo(int userId)
        {
            using (ModelToDbContext db = new ModelToDbContext())
            {
                DateTime limitTime = DateTime.Now.AddSeconds(-keepLiveSeconds);
                db.AuthorLoginInfos
                    .Where(x => x.LastHeartbeatTime > limitTime)
                    .Where(x => x.UserId == userId)
                    .ToList()
                    .ForEach(x => x.Hit = true);
                AuthorLoginInfo info = new AuthorLoginInfo(userId);
                db.AuthorLoginInfos.Add(info);
                db.SaveChanges();
                return info.Token;
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="_db">数据库连接对象</param>
        /// <param name="token">身份标识</param>
        /// <returns>返回用户信息</returns>
        public static Iworld.Author.Model.Author GetUserInfo(IModelToDbContextOfAuthor _db, string token)
        {
            using (ModelToDbContext db = new ModelToDbContext())
            {
                DateTime limitTime = DateTime.Now.AddSeconds(-keepLiveSeconds);
                AuthorLoginInfo info = db.AuthorLoginInfos
                    .Where(x => x.LastHeartbeatTime > limitTime)
                    .FirstOrDefault(x => x.Token == token && x.Hit == false);
                if (info == null)
                {
                    throw new Exception("指定的身份标识所标示的用户不存在");
                }
                return info.GetUser(_db);
            }

        }

        /// <summary>
        /// 保持心跳
        /// </summary>
        /// <param name="token"></param>
        public static void KeepHeartbeat(string token)
        {
            using (ModelToDbContext db = new ModelToDbContext())
            {
                DateTime limitTime = DateTime.Now.AddSeconds(-keepLiveSeconds);
                AuthorLoginInfo info = db.AuthorLoginInfos
                    .Where(x => x.LastHeartbeatTime > limitTime)
                    .FirstOrDefault(x => x.Token == token && x.Hit == false);
                if (info == null)
                {
                    throw new Exception("连接已断开，请重新登录");
                }
                if (info.Hit)
                {
                    throw new Exception("你的账号已经在其他地方登录，你已经被迫下线。如果不是你本人操作，请尽快修改密码。祝你好运。");
                }
                info.Heartbeat();
                db.SaveChanges();
            }
        }

        #endregion
    }
}
